package com.zmn.oms.task.listener;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.zmn.manager.mq.annotation.AliyunMQHandler;
import com.zmn.manager.mq.annotation.AliyunMQListener;
import com.zmn.manager.redis.RedisManager;
import com.zmn.oms.common.constant.MessageQueueTagConsts;
import com.zmn.oms.common.constant.RedisKeyConsts;
import com.zmn.oms.zmn.business.interfaces.work.autoflow.WorkFlowContextBService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 类描述：工单自动流转
 * @author heciqi
 * @date 2021/3/10 10:10
 */
@Slf4j
@AliyunMQListener(topic = "${spring.mq.topic.oms}")
@Component
public class WorkAutoFlowListener {

	@Autowired
	private WorkFlowContextBService workFlowContextBService;
	@Resource
	private RedisManager redisManager;

	@AliyunMQHandler(tags = MessageQueueTagConsts.WORK_AUTO_FLOW)
	public void workAutoFlow(String message) {
		log.debug("#mq#workAutoFlow#recive message:{}", message);

		Long workId = null;
		try {
			JSONObject data = JSON.parseObject(message);
			Long orderId = data.getLong("orderId");
			workId = data.getLong("workId");
			workFlowContextBService.autoFlow(orderId, workId);
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		}

		String key = String.format(RedisKeyConsts.AUTO_FLOW_LOCK, workId);
		redisManager.del(key);
	}
}
